package com.example.autoboot.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.cktim.camera2library.Camera2Config;
import com.cktim.camera2library.camera.Camera2RecordActivity;
import com.example.autoboot.R;
import com.example.autoboot.camera.Camera2RecordFinishActivity;

// camera2
public class MainActivity2 extends AppCompatActivity {
    private Button btnOpenCamera2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        btnOpenCamera2 = findViewById(R.id.btn_openCamera2);

        //配置Camera2相关参数，
        Camera2Config.RECORD_MAX_TIME = 10;
        Camera2Config.RECORD_MIN_TIME=2;
        Camera2Config.RECORD_PROGRESS_VIEW_COLOR= com.cktim.camera2library.R.color.colorAccent;
        Camera2Config.PREVIEW_MAX_HEIGHT=1300;
        Camera2Config.PATH_SAVE_VIDEO= getCacheDir() + "/CameraV2222/";
        Camera2Config.PATH_SAVE_PIC= getCacheDir() + "/CameraV2222/CameraV22222222/";
        Camera2Config.ENABLE_CAPTURE=true;

        Camera2Config.ACTIVITY_AFTER_CAPTURE = Camera2RecordFinishActivity.class;

        checkPermission(this);
        btnOpenCamera2.setOnClickListener(view -> {
            // 获取权限
            // 检查录音权限是否已经被授予
            // 直接请求权限
            ActivityCompat.requestPermissions(this,
                    new String[]{
                            Manifest.permission.RECORD_AUDIO,
                            Manifest.permission.CAMERA,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE,
                    },
                    123);
            // 跳转
            Intent intent = new Intent(this, Camera2RecordActivity.class);
            startActivity(intent);
        });
    }

    public static void checkPermission(AppCompatActivity activity) {
        if (Build.VERSION.SDK_INT >= 23) {
            int checkPermission =
                    ContextCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO)
                            + ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE)
                            + ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                            + ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
            if (checkPermission != PackageManager.PERMISSION_GRANTED) {
                //动态申请
                ActivityCompat.requestPermissions(activity, new String[]{
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE}, 123);
            }
        }
    }
}
